﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;

namespace Nirvana
{
    public static class UnitySystemTool
    {

        /// <summary>
        /// 解析打包信息
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public  static string ParseBuildInfo(string tag)
        {
            if (string.IsNullOrEmpty(tag))
            {
                return tag;
            }
            DateTime now = DateTime.Now;
            tag = tag.Replace("{now}", now.ToString());
            tag = tag.Replace("{nowTicks}", now.Ticks.ToString());
            tag = tag.Replace("{unityVersion}", Application.unityVersion);
            tag = tag.Replace("{appVersion}", Application.version);
            return tag;
        }
        public static string Replace(string tag, string flag, string file)
        {
            if (!string.IsNullOrEmpty(file)) 
            {
                return tag.Replace(flag, file);
            }
            if (!tag.Contains(flag))
            {
                return tag;
            }
            string text = "/" + flag;
            if (tag.Contains(text))
            {
                return tag.Replace(text, string.Empty);
            }
            return tag.Replace(flag, string.Empty);
        }

        /// <summary>
        /// 解析Sprite的Tag信息
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static string ParseSpriteTag(string tag,string filepath)
        {
            if(string.IsNullOrEmpty(tag))
            {
                return tag;
            }

            string filename = Path.GetFileNameWithoutExtension(filepath);
            tag = tag.Replace("{filename}", filename); 
            string dirname = Path.GetDirectoryName(filepath);
            string lastdir = Path.GetFileName(dirname);
            tag = tag.Replace("{lastdir}", lastdir);
            string[] array = filepath.Split('/', '\\');

            string firstdir = string.Empty, seconddir = string.Empty, thirddir = string.Empty;
            string fourthdir = string.Empty;

            if (array.Length > 0 && array[0] != Path.GetFileName(filepath))
            {
                firstdir = array[0];
            }
            if (array.Length > 1 && array[1] != Path.GetFileName(filepath))
            {
                seconddir = array[1];
            }
            if (array.Length > 2 && array[2] != Path.GetFileName(filepath))
            {
                thirddir = array[2];
            }
            if (array.Length > 3 && array[3] != Path.GetFileName(filepath))
            {
                fourthdir = array[3];
            }
            tag = Replace(tag, "{firstdir}", firstdir);//在AssetImportRule配置文件目录下的第一个子目录开始
            tag = Replace(tag, "{seconddir}", seconddir);
            tag = Replace(tag, "{thirddir}", thirddir);
            tag = Replace(tag, "{fourthdir}", fourthdir);
            return tag;
        }

    }

}
